package carltechnology.com.mahjonggame.presenter.impl;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import carltechnology.com.mahjonggame.R;
import carltechnology.com.mahjonggame.mode.IData;
import carltechnology.com.mahjonggame.presenter.IMahjongGameDetailPresenter;
import carltechnology.com.mahjonggame.utils.dao.impl.MahjongDatabaseManager;
import carltechnology.com.mahjonggame.views.IMahjongGameDetailView;
import rx.Subscriber;
import rx.Subscription;

/**
 * Created by WZTCM on 2016/6/14.
 */
public class MahjongGameDetailPresenter implements IMahjongGameDetailPresenter {
    //private static MahjongGameDetailPresenter mahjongGameDetailPresenter;


    private IMahjongGameDetailView mDetailView;
    private MahjongDetailAdapter adapter;
    private Subscription subscription;

    private MahjongGameDetailPresenter(IMahjongGameDetailView view) {
        mDetailView = view;
        adapter = new MahjongDetailAdapter(mDetailView.getContext());
        mDetailView.getRecycleView().setLayoutManager(new LinearLayoutManager(mDetailView.getContext()));
        mDetailView.getRecycleView().setAdapter(adapter);
    }

    @Override
    public void endFragment() {
        subscription.unsubscribe();
    }


    public static MahjongGameDetailPresenter getInstance(IMahjongGameDetailView view, final int id) {
        MahjongGameDetailPresenter tmp = new MahjongGameDetailPresenter(view);
        tmp.getMahjongGameAllInfoDetail(id);
        return tmp;
    }

    private Subscriber<IData> getSubs() {
        return new Subscriber<IData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(IData data) {
                adapter.add(data);
            }
        };
    }

    @Override
    public void getMahjongGameAllInfoDetail(final int id) {
        subscription = MahjongDatabaseManager.getInstance().getMahjongGameDetailInfo(getSubs(), id);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.mahjonggame_detail_label)
        TextView labelTextView;
        @InjectView(R.id.mahjonggame_detail_money1)
        TextView money1TextView;
        @InjectView(R.id.mahjonggame_detail_money2)
        TextView money2TextView;
        @InjectView(R.id.mahjonggame_detail_money3)
        TextView money3TextView;
        @InjectView(R.id.mahjonggame_detail_money4)
        TextView money4TextView;
        @InjectView(R.id.mahjonggame_detail_cost)
        TextView costTextView;
        @InjectView(R.id.mahjonggame_detail_date)
        TextView dateTextView;
        @InjectView(R.id.mahjonggame_detail_perunit)
        TextView perunitTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        public TextView getLabelTextView() {
            return labelTextView;
        }

        public TextView getMoney1TextView() {
            return money1TextView;
        }

        public TextView getMoney3TextView() {
            return money3TextView;
        }

        public TextView getMoney2TextView() {
            return money2TextView;
        }

        public TextView getMoney4TextView() {
            return money4TextView;
        }

        public TextView getCostTextView() {
            return costTextView;
        }

        public TextView getDateTextView() {
            return dateTextView;
        }

        public TextView getPerunitTextView() {
            return perunitTextView;
        }
    }

    class MahjongDetailAdapter extends RecyclerView.Adapter<MahjongGameDetailPresenter.MyViewHolder> {
        private List<IData> list;
        private final Context context;
        private final LayoutInflater mLayoutInflater;

        public MahjongDetailAdapter(Context context) {
            list = new CopyOnWriteArrayList<>();
            this.context = context;
            mLayoutInflater = LayoutInflater.from(context);
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(mLayoutInflater.inflate(R.layout.mahjonggame_content_detail_item, parent, false));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            IData data = list.get(position);
            holder.getLabelTextView().setText(position + ".");
            holder.getMoney1TextView().setText(data.getPrice()[0] + "元");
            holder.getMoney2TextView().setText(data.getPrice()[1] + "元");
            holder.getMoney3TextView().setText(data.getPrice()[2] + "元");
            holder.getMoney4TextView().setText(data.getPrice()[3] + "元");
            holder.getCostTextView().setText(data.getCost() + "元");
            holder.getPerunitTextView().setText(data.getCost() + "元");
            holder.getDateTextView().setText(data.getDate());
        }

        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        public void add(IData data) {
            list.add(data);
            notifyDataSetChanged();
        }

        public void clear() {
            list.clear();
            notifyDataSetChanged();
        }

        public void addAll(List<IData> list) {
            list.addAll(list);
            notifyDataSetChanged();
        }

        public void remove(int location) {
            list.remove(location);
            notifyDataSetChanged();
        }
    }
}
